package com.rumor.background.Service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rumor.background.Service.WeChatLoginService;
import com.github.kevinsawicki.http.HttpRequest;
import com.rumor.background.Service.WeChatLoginService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WeChatLoginServiceImpl implements WeChatLoginService {

    @Override
    public String code2openid(String code) {
        Map<String, String> data = new HashMap<String, String>();
        data.put("appid", "wxe8c06c5c275f32dc");
        data.put("secret", "9a7c38e2bde7517a7c8a8b07bddc0685");
        data.put("js_code", code);
        data.put("grant_type", "authorization_code");

        String response = HttpRequest.get("https://api.weixin.qq.com/sns/jscode2session").form(data).body();
//        System.out.println("Response was: " + response);
        JSONObject obj= JSON.parseObject(response);//将json字符串转换为json对
//        System.out.println(obj);
        return obj.getString("openid");
    }
}